package com.lxz.shardingjdbcdemo.controller;

import com.lxz.shardingjdbcdemo.dao.UserMapper;
import com.lxz.shardingjdbcdemo.model.User;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Resource
    private UserMapper userMapper;

    /**
     * 添加用户
     */
    @PostMapping("/add")
    public String addUser(@RequestBody User user) {
        userMapper.insert(user);
        return "User added successfully";
    }

    /**
     * 获取所有用户
     */
    @GetMapping("/all")
    public List<User> getAllUsers() {
        return userMapper.selectList(null);
    }

    /**
     * 根据ID获取用户
     */
    @GetMapping("/{userId}")
    public User getUserById(@PathVariable Long userId) {
        return userMapper.selectById(userId);
    }
}